<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="bootstrap.css">
    <style>
        .list-group-item {
            margin-bottom: 5px;
        }
    </style>
</head>
<body>
<div class="container">
	<div class="alert alert-success text-center">
		<h2>刷新页面不会更新更改的内容</h2>
	</div>
    <div class="panel panel-info">
        <div class="panel-heading">
            <h3>花里胡哨 <input type="button" value="点击增加" class="btn btn-primary"></h3>
        </div>
        <div class="panel-body">
            <ul class="list-group">
            </ul>
        </div>
        <div class="panel-footer">
            <p>&copy;花里胡哨</p>
        </div>
    </div>
</div>
<script src="jquery.js"></script>
<script>
    var table = localStorage.table;
    if (table == undefined) {
        table = [];
    } else {
        table = JSON.parse(table);
    }
    var list = $(".list-group");
    table.forEach(function (v) {
        list.append(v['li']);
    })

    function save() {
        localStorage.table = JSON.stringify(table);
    }

    var num = 0;
    $(".btn").on('click', function () {
        num++;
        var li = $("<li class='list-group-item' contenteditable='true'>花里胡哨" + num + " <span class='close'>&times;</span></li>");
        table.push({
            'li': li[0].outerHTML//包括本身的所有节点,innerHTML是子节点
        });
        save();
        li.appendTo('.list-group');
    });
	
    $(".list-group").on('click', '.close', function () {
        var parent = $(this).parent();
        var index = $(this).parent().index();
        table.splice(index, 1);
        save();
        parent.remove();
    });
    $('.list-group').on('input','.list-group-item', function () {
        var index = $(this).index();
        table[index]['li'] = this.outerHTML;
        save();
    })
</script>
</body>
</html>